package com.luo.chengrui.module.sa.service.datapermission;

import com.luo.chengrui.module.sa.service.datapermission.DataPermissionItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionItemDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import com.luo.chengrui.module.sa.convert.datapermission.DataPermissionItemConvert;
import com.luo.chengrui.module.sa.dal.mysql.datapermission.DataPermissionItemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.luo.chengrui.module.sa.enums.ErrorCodeConstants.*;;

/**
 * 数据权限项，每一项对应一个业务权限场景 Service 实现类
 *
 * @author 诚睿
 */
@Service
@Validated
public class DataPermissionItemServiceImpl implements DataPermissionItemService {

    @Resource
    private DataPermissionItemMapper dataPermissionItemMapper;

    @Override
    public String createDataPermissionItem(DataPermissionItemCreateReqVO createReqVO) {
        createReqVO.setVersion(System.currentTimeMillis());
        // 插入
        DataPermissionItemDO dataPermissionItem = DataPermissionItemConvert.INSTANCE.convert(createReqVO);

        validateCode(dataPermissionItem);
        dataPermissionItemMapper.insert(dataPermissionItem);
        // 返回
        return dataPermissionItem.getId();
    }

    @Override
    public void updateDataPermissionItem(DataPermissionItemUpdateReqVO updateReqVO) {
        // 校验存在
        validateDataPermissionItemExists(updateReqVO.getId());
        updateReqVO.setVersion(System.currentTimeMillis());
        // 更新
        DataPermissionItemDO updateObj = DataPermissionItemConvert.INSTANCE.convert(updateReqVO);
        validateCode(updateObj);
        dataPermissionItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteDataPermissionItem(String id) {
        // 校验存在
        validateDataPermissionItemExists(id);
        // 删除
        dataPermissionItemMapper.deleteById(id);
    }

    private void validateDataPermissionItemExists(String id) {
        if (dataPermissionItemMapper.selectById(id) == null) {
            throw exception(DATA_PERMISSION_ITEM_NOT_EXISTS);
        }
    }

    private void validateCode(DataPermissionItemDO item) {
        DataPermissionItemDO dataPermissionItem = dataPermissionItemMapper.selectBySubCode(item);
        if (dataPermissionItem == null) {
            return;
        }
        if (item.getId() == null || !Objects.equals(item.getId(), dataPermissionItem.getId())) {
            throw exception(DATA_PERMISSION_ITEM_CODE_EXISTS);
        }
    }

    @Override
    public DataPermissionItemDO getDataPermissionItem(String id) {
        return dataPermissionItemMapper.selectById(id);
    }

    @Override
    public List<DataPermissionItemDO> getDataPermissionItemList(Collection<String> ids) {
        return dataPermissionItemMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DataPermissionItemDO> getDataPermissionItemPage(DataPermissionItemPageReqVO pageReqVO) {
        return dataPermissionItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DataPermissionItemDO> getDataPermissionItemList(DataPermissionItemExportReqVO exportReqVO) {
        return dataPermissionItemMapper.selectList(exportReqVO);
    }

}
